<template>
  <div>
    <Menu mode="horizontal" @on-select="onSelectMenu" theme="light" :active-name="searchParam.loginType">
      <MenuItem name="student">
        学生记录
      </MenuItem>
      <MenuItem name="kinsfolk">
        家长记录
      </MenuItem>
      <MenuItem name="teacher">
        教师记录
      </MenuItem>
    </Menu>
    <Row>
      <Card>
        <VmBtable type="edit" ref="VmBtable"
                  :columns="dataColumns"
                  :data="dataTable"
                  :searchparam="searchParam"
                  :exportBtn="false"
                  :importBtn="false"
                  :batchDelBtn="false"
                  v-on:searchFn="searchFn"
                  v-on:page-change="pageChangeFn"
                  v-on:addFn="addFn"
                  v-on:editFn="editFn"
                  v-on:deleteFn="deleteFn"
                  :downTemplate="false"
                  :operate="operate"
                  :addBtn="false"
                  placeholder="姓名关键字"
                  searchText="姓名："
                  cleanText="刷新"
                  v-on:restFn="restFn">
          <div slot="search" class="db_inline">
            <div>
              进出方向：
              <Select v-model="searchParam.direction" style="width:120px">
                <Option v-for="item in directionList" :value="item.value" :key="item.value">{{ item.label }}</Option>
              </Select>
              时间段：
              <DatePicker type="datetime" v-model="searchParam.bTime" @on-change="searchParam.bTime=$event"
                          placeholder="开始时间" style="width: 170px"></DatePicker>
              至
              <DatePicker type="datetime" v-model="searchParam.eTime" @on-change="searchParam.eTime=$event"
                          placeholder="结束时间" style="width: 170px"></DatePicker>
            </div>
          </div>
          <div slot="operation">
            <Button type="info" @click="exportFn">
              <Icon type="connection-bars"></Icon>
              <span>导出</span>
            </Button>
          </div>
        </VmBtable>
      </Card>
    </Row>
    <Modal v-model="modal.visible" :title="modal.title" v-on:on-ok="subForm" :mask-closable="false" width="540"
           v-on:on-cancel="cancelSub">
      <Form ref="Form" :model="modal.form" :label-width="100" label-position="right" :rules="modal.rules">
        <FormItem label="名称" prop="name">
          <Input v-model="modal.form.name" placeholder="亲输入幼儿园名称" style="width:63%"/>
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="cancelSub">取消</Button>
        <Button type="primary" @click="subForm" :loading="modal.loading">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
  import API from "@/api/api";
  import VmBtable from "@/components/vm/vm-btable";

  export default {
    components: {
      VmBtable
    },
    data() {
      return {
        searchParam: {
          current: 1,
          size: 20,
          total: 0,
          name: '',
          direction: '',
          loginType: 'student',
          bTime: '',
          eTime: '',
        },
        dataColumns: [],
        studentColumns: [
          {
            title: '序号',
            type: 'index',
            align: 'center',
            width: 100
          },
          {
            title: '姓名',
            key: 'name',
            align: 'center',
          },
          {
            title: '时间',
            key: 'signTime',
            align: 'center',
            sortable: true,
            sortType: 'desc'
          },
          {
            title: '年级',
            key: 'gradeName',
            align: 'center',
          },
          {
            title: '班级',
            key: 'clazzName',
            align: 'center',
          },
          {
            title: '方向',
            key: 'direction',
            align: 'center',
          },
          {
            title: '刷卡/刷脸',
            key: 'type',
            align: 'center',
          },
        ],
        otherColumns: [
          {
            title: '序号',
            type: 'index',
            align: 'center',
            width: 100
          },
          {
            title: '姓名',
            key: 'name',
            align: 'center',
          },
          {
            title: '时间',
            key: 'signTime',
            align: 'center',
            sortable: true,
            sortType: 'desc'
          },
          {
            title: '方向',
            key: 'direction',
            align: 'center',
          },
          {
            title: '刷卡/刷脸',
            key: 'type',
            align: 'center',
          },

        ],
        operate: [],
        dataTable: [],
        directionList: [
          {value: '', label: '全部'},
          {value: '出园', label: '出园'},
          {value: '进园', label: '进园'},
        ],
        modal: {
          visible: false,
          title: '',
          isEdit: false,
          loading: false,
          form: {
            name: '',
          },
          rules: {
            'name': [{
              required: true,
              trigger: 'blur',
              message: '必填项'
            }]
          }
        },
      };
    },
    methods: {
      onSelectMenu(name) {
        this.searchParam.loginType = name;
        this.dataColumns = name === 'student' ? this.studentColumns : this.otherColumns;
        this.queryFn();
      },
      exportFn(){
        this.$Modal.confirm({
          title: '提示',
          content: '是否导出符合筛选条件的考勤数据？',
          onOk: () => {
            API.exportGateRecord(this.searchParam);
          },
          onCancel: () => {

          }
        })
      },
      addFn() {
        Object.assign(this.modal, {
          visible: true,
          isEdit: false,
          title: '添加',
          form: {}
        })
      },
      editFn(data) {
        Object.assign(this.modal, {
          visible: true,
          isEdit: true,
          title: '编辑',
          form: data
        })
      },
      deleteFn(data) {

      },
      restFn() {
        this.searchParam.current = 1;
        this.searchParam.size = 20;
        this.searchParam.name = '';
        this.searchParam.direction = '';
        this.searchParam.bTime = '';
        this.searchParam.eTime = '';
        this.queryFn();
      },
      queryFn() {
        API.fetchGateRecord(this.searchParam).then(
          res => {
            let data = res.data;
            this.dataTable = data.records;
            this.searchParam.total = data.total;
          },
          error => {
            this.$Message.info(error.msg);
          }
        );
      },
      complete() {
        Object.assign(this.modal, {
          visible: false,
          title: '',
          loading: false,
        });
        this.$refs.Form.resetFields();
      },
      subForm() {
        this.$refs.Form.validate(valid => {
          if (valid) {
            let data = Object.assign(this.modal.form);
            if (data.id) {

            } else {

            }
          }
        })
      },
      cancelSub() {

      },
      pageChangeFn(current) {
        this.searchParam.current = current;
        this.queryFn();
      },
      searchFn(value) {
        this.searchParam.name = value;
        this.queryFn();
      },
    },
    created() {
    },
    mounted() {
      this.queryFn();
      this.dataColumns = this.studentColumns;
    }
  }
</script>

<style scoped>

</style>
